package com.itheima.Demo;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import com.sun.org.apache.xalan.internal.xsltc.runtime.InternalRuntimeError;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class Demo1 {
    @Test
    //测试代理方式实现的遍历
    public void testFindAll() throws Exception {
        // 读取核心配置文件
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //构建工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        // 获取session对象
        SqlSession session = build.openSession();
        // 通过session获取接口的代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        // 分页助手
//        PageHelper.startPage(1,3);
        PageHelper.startPage(2,3);

        // 调用findAll方法
        List<User> list = mapper.findAll();
        //  打印输出

        for (User user : list) {
            System.out.println(user);
        }

        //获取分页相关参数
        PageInfo<User> info=new PageInfo<>(list);
        System.out.println("总条数:"+info.getTotal());

        //提交事务
        session.commit();
        //   释放资源
        session.close();
        is.close();

    }


}
